/**
 * 
 */
package org.swing.utility.common.chars;

/**
 * @author lqnhu
 *
 */
public class ByteArrayReadOnlyAsciiBuf extends ByteArrayReadOnlyUtfBuf {
	public ByteArrayReadOnlyAsciiBuf(byte[] data) {
		super(data);
	}

	public ByteArrayReadOnlyAsciiBuf(String javaString) {
		super(javaString);
	}

	
	protected boolean knownAsciiOnly(int cacheInstance) {
		return true;
	}

	
	public int length() {
		return getByteLength();
	}

	
	public char charAt(int index) {
		return (char) getByte(index);
	}

	// start is inclusive, end is exclusive
	
	public ReadableCharBuf subSequence(int start, int end) {
		return getSubSequenceForByteBounds(start, end);
	}

	
	public String toString() {
		return new String(data, 0);
	}

	/**
	 * Uses the cacheInstance for caching the hash instead
	 */
	
	public int hashCode() {
		int hash = packedIndexCache;
		if (hash == 0) {
			int length = length();
			for (int i = 0; i < length; i++) {
				char c = charAt(i);
				hash = 31 * hash + c;
			}
			packedIndexCache = hash;
		}
		return hash;
	}
}
